﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

using BotonJXC.BLL;
using BotonJXC.Entity;
using BotonJXC.DAO;
using BotonJXC.Util;
using NHibernate;

namespace BotonJXC.Forms.Controls
{
    public partial class StoreControl : BaseInfoControl
    {
        StoreBLL _storeBll = new StoreBLL();
        RegionBLL _regionBll = new RegionBLL();
        string _qStoreName = "";
        short _qStoreLevel = 0;
        IList<Store> _storeDatas;
        IList<Entity.Region> regions;
        public StoreControl()
        {
            InitializeComponent();
        }

        private void StoreControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                IList<ComboBoxDropItem> items = OrgLevelUtil.CreateOrgLevelList();
                kcbStoreLevel.DataSource = items;
                LoadData();
                foreach(ComboBoxDropItem item in items)
                {
                    this.kdgvcbcStoreLevel.Items.Add(item.Text);
                }

                regions = _regionBll.FindAll();
                foreach (Entity.Region r in regions)
                {
                    kdgvcbcRegion.Items.Add(r.RegionName);
                }
            }
        }
        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvStore.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvStore.Rows)
                {
                    Store store = r.DataBoundItem as Store;
                    if (store == null)
                        continue;
                    if (store.StoreId == 0)
                    {
                        _storeBll.Save(store);
                    }
                    else if ("1".Equals(r.Tag) && store.StoreId != 0)
                    {
                        _storeBll.Update(store);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void AddInfo()
        {
            IList<Store> units = (IList<Store>)kdgvStore.DataSource;
            Store u = new Store();
            units.Add(u);
            kdgvStore.Refresh();
        }

        public override void DeleteInfo()
        {
            IList<Store> units = (IList<Store>)kdgvStore.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvStore.SelectedRows)
                {
                    Store unit = r.DataBoundItem as Store;
                    if (unit != null)
                    {
                        _storeBll.Delete(unit.StoreId);
                    }
                    units.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvStore.Refresh();
        }
        private void LoadData()
        {
            PageModel<Store> pageModel = _storeBll.Find(_qStoreName, _qStoreLevel, 0, 10);
            _storeDatas = pageModel.Results;
            this.pager1.TotalCount = pageModel.TotalCount;
            kdgvStore.DataSource = new BindingList<Store>(_storeDatas);
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _qStoreName = ktbStoreName.Text;
            if (kcbStoreLevel.SelectedValue != null)
                _qStoreLevel = (short)kcbStoreLevel.SelectedValue;
            else
                _qStoreLevel = 0;
            LoadData();
        }

        private void kdgvStore_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string colName = kdgvStore.Columns[kdgvStore.CurrentCell.ColumnIndex].Name;
            if (colName == "kdgvcbcStoreLevel" || colName == "kdgvcbcRegion" || colName == "DCStoreType")
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<Store> storeDatas = (IList<Store>)kdgvStore.DataSource;
            string colName = kdgvStore.Columns[kdgvStore.CurrentCell.ColumnIndex].Name;
            if (colName == "kdgvcbcStoreLevel")
            {
                storeDatas[kdgvStore.CurrentCell.RowIndex].StoreLevel = (short)(cbc.SelectedIndex + 1);
            }
            else if (colName == "kdgvcbcRegion")
            {
                storeDatas[kdgvStore.CurrentCell.RowIndex].Region = regions[cbc.SelectedIndex];
            }
            else if (colName == "DCStoreType")
            {
                if (cbc.SelectedIndex == 2)
                {
                    storeDatas[kdgvStore.CurrentCell.RowIndex].StoreType = 4;
                }
                else
                {
                    storeDatas[kdgvStore.CurrentCell.RowIndex].StoreType = (short)(cbc.SelectedIndex + 1);
                }                
            }
        }

        private void kdgvStore_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvStore.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvStore_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (_storeDatas.Count > e.RowIndex)
            {
                Store store = _storeDatas[e.RowIndex];
                if (kdgvStore.Columns[e.ColumnIndex].Name == "kdgvcbcStoreLevel")
                {
                    e.Value = store.StoreLevel + "级";
                }
                else if (kdgvStore.Columns[e.ColumnIndex].Name == "kdgvcbcRegion" && store.Region != null)
                {
                    e.Value = store.Region.RegionName;
                }
                else if (kdgvStore.Columns[e.ColumnIndex].Name == "DCStoreType")
                {
                    if (store.StoreType == 1)
                    {
                        e.Value = "普通门店";
                    }
                    else if (store.StoreType == 2)
                    {
                        e.Value = "配送中心";
                    }
                    else
                    {
                        e.Value = "加工间";
                    }
                }
            }

        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}